package cn.niit.dao;

import cn.niit.entity.SysUserRole;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface SysUserRoleMapper {
    @Insert({
        "insert into t_sys_user_role (id, user_id, ",
        "role_id, gmt_create, ",
        "gmt_update, create_user_id, ",
        "create_user_name, modify_user_id, ",
        "modify_user_name)",
        "values (#{id,jdbcType=BIGINT}, #{userId,jdbcType=BIGINT}, ",
        "#{roleId,jdbcType=BIGINT}, #{gmtCreate,jdbcType=TIMESTAMP}, ",
        "#{gmtUpdate,jdbcType=TIMESTAMP}, #{createUserId,jdbcType=BIGINT}, ",
        "#{createUserName,jdbcType=VARCHAR}, #{modifyUserId,jdbcType=BIGINT}, ",
        "#{modifyUserName,jdbcType=VARCHAR})"
    })
    int insert(SysUserRole record);

    int insertSelective(SysUserRole record);

    /**
     * 根据userID获取当前用户所有角色
     * @param userId
     * @return
     */
    @Select("select r.id,r.role_name as roleName \n" +
            "from t_sys_role r, t_sys_user_role ur \n" +
            "where r.id = ur.role_id and ur.user_id = #{userId} order by r.id")
    List<SysUserRole> getAllByUserId(@Param("user_id") Long userId);
}